<?php
class Logs_model extends MY_Model{
	var $operate_id;
	var $uid;
	var $cid;
	var $content = 'gao mao a ';
	var $remark;
	var $created;
	var $changed;
	var $status;
	
	function __construct($id=''){
		parent::__construct();
		$GLOBALS['table'] = 'logs';
		if($id){
			$query = $this->db->get_where('logs',array('id' => $id),1);
			$result = $query->result();
			if(!empty($result)){
				$this->id = $result[0]->id;
				$this->operate_id = $result[0]->operate_id;
				$this->uid = $result[0]->uid;
				$this->cid = $result[0]->cid;
				$this->content = $result[0]->content;
				$this->remark = $result[0]->remark;
				$this->created = $result[0]->created;
				$this->changed = $result[0]->changed;
				$this->status = $result[0]->status;
			}
		}
	}
	
	
	function l($operate_id,$category_id='',$key=''){
		$cat_name = $this->cat_id_to_name($category_id);
		$this->operate_id = $operate_id;
		switch($operate_id){
			case '1':
				$this->content = "新建".$cat_name.$key;
				break;
			case '2':
				$this->content = "修改".$cat_name.$key;
				break;
			case '3':
				$this->content = "删除".$cat_name.$key;
				break;
			case '4':
				$this->content = "分配".$cat_name."——".$key;
				break;
			case '5':
				$this->content = "卸载入库".$cat_name."——".$key;
				break;
			case '6':
				$this->content = "卸载报废".$cat_name."——".$key;
				break;
		}
		$this->created = $this->changed = time();
		$this->status = '1';
	}
	
	function cat_id_to_name($category_id=''){
		switch($category_id){
			case '0':
				return "主机配件";
				break;
			case '1':
				return "主机";
				break;
			case '2':
				return "外设";
				break;
			case '3':
				return "书本";
				break;
			case '4':
				return "耗材";
				break;
			default:
				return "员工";
		}
	}
	
	function get_all_rows($where){
		if($where['from']){
			$this->db->where('changed >=',strtotime($where['from']));
		}
		if($where['to']){
			$this->db->where('changed <=',strtotime($where['to']));
		}
		if($where['key']){
			$this->db->like('content',$where['key']);
		}
		return $this->db->count_all_results('logs');
	}
	
	function get_logs_by_page($limit,$offset,$where){
		if($where['from']){
			$this->db->where('changed >=',strtotime($where['from']));
		}
		if($where['to']){
			$this->db->where('changed <=',strtotime($where['to']));
		}
		if($where['key']){
			$this->db->like('content',$where['key']);
		}
		$query = $this->db->get('logs',$limit,$offset);
		return $query->result_array();
	}
}